package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

/**
 * 广东大华网新闻 解析类
 *
 * @author Chase
 *
 */
public class DaHuaWangNewsAnalyse implements AnalyseNews {

	public static void main(String[] args) {
		DaHuaWangNewsAnalyse parser=new DaHuaWangNewsAnalyse();
		String Url="http://money.dahuawang.com/news/130805/1/story_161751.html";
		UrlMeta oneMeta=CrawlHTML.responseToURL(Url);
		System.out.println(parser.parserHtml(oneMeta));
	}

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://dahuawang.com/gundong/showfirst1.asp[?]ID=[0-9]*&(Page=&)?CNo=[0-9]{4}",
				"http://[a-zA-Z]*.dahuawang.com/gundong/showfirst1.asp[?]ID=[0-9]*&(Page=&)?CNo=[0-9]{4}",
				"http://dahuawang.com/gundong/showfirst1.asp[?]id=[0-9]*&(Page=&)?CNo=[0-9]{4}",
				"http://[a-zA-Z]*.dahuawang.com/gundong/showfirst1.asp[?]id=[0-9]*&(Page=&)?CNo=[0-9]{4}",
				"http://dahuawang.com/dhserver/writer/content.asp[?]ID=[0-9]*&CNo=[0-9]*",
				"http://[a-zA-Z]*.dahuawang.com/dhserver/writer/content.asp[?]id=[0-9]*&CNo=[0-9]*",
				"http://money.dahuawang.com/news/[0-9]{6}/[0-9]{1}/story_[0-9]*.html",
				"http://auto.dahuawang.com/news/[0-9]{8}/story_[0-9]*.html",
				"http://sttb.dahuawang.com/news[_0-9]*.html"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url=urlMeta.getUrl();

		String html=urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title=null;
		String content=null;
		Long dateLong=null;
		String commentNum=null;
		String clickNum=null;

		Document doc=Jsoup.parse(html);
		NewsMeta meta=new NewsMeta();
		if(url.startsWith("http://auto.dahuawang.com/")){
			title=doc.select("div.ina_news_text>h1>p>b").text();// 标题
			dateLong=DateUtils.matchDate(doc.select("div.ina_news_text>h1>span").text());// 时间
			content=HtmlCleaner.getContentHtml(url,doc.select("div.ina_news_contents")); //内容
		} else if(url.startsWith("http://money.dahuawang.com/")){
			title=doc.select("div.cs_title>h1").text();// 标题
			dateLong=DateUtils.matchDate(doc.select("em").text());// 时间
			content=HtmlCleaner.getContentHtml(url,doc.select("div.article")); //内容
//			commentNum=doc.select("span#DRed tieTotalCount").text(); //评论数
		} else if(url.startsWith("http://sttb.dahuawang.com/")){
			title=doc.select("h3").text();// 标题
			content=HtmlCleaner.getContentHtml(url,doc.select("div.Article p")); //内容
			dateLong=DateUtils.matchDate(doc.select("h5").text());
			if(dateLong == null){
				dateLong=DateUtils.matchDate(content);// 时间
			}
//			commentNum=doc.select("span#DRed tieTotalCount").text(); //评论数
		} else {
			title=doc.select("h4").text();// 标题
			dateLong=DateUtils.matchDate(doc.select("span.pdate").text());// 时间
			content=HtmlCleaner.getContentHtml(url,doc.select("div.Content")); //内容
//			commentNum=doc.select("span#DRed tieTotalCount").text(); //评论数
		}
		if (dateLong == null) {
			dateLong=DateUtils.matchDate(doc.select("div.bigBox").text());// 时间
		}
		if (dateLong==null){
			dateLong=JSoupUtils.matchDate(doc, "发表日期");
		}
		if(dateLong==null){
			dateLong=DateUtils.matchDate(doc.select("div.cs_title p span:eq(1)").text());
		}



		meta.setTitle(title);
		meta.setUrl(url);

		meta.setType(1);
		meta.setClickNum(NumberUtils.parseInt(clickNum));
		meta.setCommentNum(NumberUtils.parseInt(commentNum));
		meta.setContent(content);
		meta.setDate(dateLong);

		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
